<template>
    <div class="dashboard-container">
        <div class="app-container">
            <div class="edit-form">
                <el-form ref="userForm" :model="userInfo" :rules="rules" label-width="220px">
                    <!-- 姓名-->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="姓名" prop="username">
                                <el-input v-model="userInfo.username" size="mini" class="inputW" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 工号 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="工号" prop="workNumber">
                                <el-input v-model="userInfo.workNumber" size="mini" class="inputW" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 手机  -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="手机" prop="mobile">
                                <el-input v-model="userInfo.mobile" size="mini" class="inputW" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 部门 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="部门" prop="departmentId">
                                <!-- 放置级联部门组件 会单独封装 -->
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 聘用形式 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="聘用形式" prop="formOfEmployment">
                                <el-select v-model="userInfo.formOfEmployment" size="mini" class="inputW">
                                    <el-option label="正式" :value="1" />
                                    <el-option label="实习生" :value="2" />
                                    <el-option label="兼职" :value="3" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 入职时间 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="入职时间" prop="timeOfEntry">
                                <el-date-picker 
                                    v-model="userInfo.timeOfEntry" 
                                    size="mini" 
                                    type="date" 
                                    value-format="yyyy-MM-dd" 
                                    class="inputW" 
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 转正时间 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="转正时间" prop="correctionTime">
                                <el-date-picker 
                                    v-model="userInfo.correctionTime"
                                    size="mini" 
                                    type="date" 
                                    value-format="yyyy-MM-dd" 
                                    class="inputW" 
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 员工照片 -->
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="员工头像">
                                <!-- 放置上传图片 -->
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <!-- 保存个人信息 -->
                    <el-row type="flex">
                        <el-col :span="12" style="margin-left: 220px;">
                            <el-button size="mini" type="primary" @click="saveData">保存更新</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            userInfo: {
                username: '',// 用户名
                mobile: '',//手机号
                workNumber: '',
                formOfEmployment: null,
                departmentId: null,
                timeOfEntry: '',
                correctionTime: ''
            },
            rules: {
                username: [{ required: true, message: '请输入姓名', trigger: 'blur' }, { min: 1, max: 4, message: '姓名为1-4个字符' }],
                mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, {
                    //pattern正则表达式
                    pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
                }],
                formOfEmployment: [{ required: true, message: '请选择聘用方式', trigger: 'blur' }],
                departmentId: [{ required: false, message: '请选择部门', trigger: 'blur' }],
                timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }],
                correctionTime: [{ required: true, message: '请选择转正时间', trigger: 'blur' }, {
                    validator: (rule, value, callback) => {
                        if (this.userInfo.timeOfEntry) {
                            // 入职过
                            if (new Date(this.userInfo.timeOfEntry) > new Date(value)) {
                                callback(new Error('转正时间不能小于入职时间!'))
                                return
                            }
                        }
                        callback()
                    }
                }],
            }
        }
    },
    methods: {
        saveData() {
            this.$refs.userForm.validate()
        }
    }
}
</script>

<style lang="scss" scoped>
.edit-form {
    background-color: #fff;
    padding: 20px;

    .inputW {
        width: 380px;
    }
}
</style>